<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\DB;

class BuildingMongo extends \Mongo
{
    protected $collection = 'buildings';
    protected $connection = 'mongodb';
    protected $fillable = [];


    public static function create($arr)
    {
       $res =  DB::connection('mongodb')
            ->collection('buildings')
            ->insert($arr);
       if ($res){
           return ['code'=>200,'message'=>'添加成功','data'=>['_id'=>$res]];
       }else{
           return ['code'=>400,'message'=>'添加失败','data'=>[]];
       }
    }

    public static function select($province)
    {
        if (isset($province)){
            //查询 变量的类型需要与数据库保持一致
            $province = intval($province);
            $data =  DB::connection('mongodb')->collection('buildings')->where('province',$province)->get();
        }else{
            $data = DB::connection('mongodb')->collection('buildings')->get();   //查询所有数据
        }
        return $data;
    }


}
